<?php
/*
 * JAdmin_HTML_PHP.php
 * name:xiaojia
 * email:iatt@qq.com
 * qq:273142650
 * time:2011.9.20 16:42
*/
	class JAdmin_HTML
	{
		
		function but_img ($style, $title = '', $href = NULL)
		{
			if ($href == NULL) {
				$href = '';
			} else {
				$href = 'onclick="toPage(\''.$href.'\');"';
			}
			if (is_int($style)) {
				$class = 'icon_' . $style;
				$return = '<div class="but_img"><div class="'.$class.'"></div><div class="icon_title"><a href="javascript:;" '.$href.'>'.$title.'</a></div></div>';
			} else {
				$return = '<div class="but_img"><div class="icon_img"><img src="'.$style.'"/></div><div class="icon_title"><a href="javascript:;" '.$href.'>'.$title.'</a></div></div>';
			}
			
			return $return;
		}
		
		function table ($title = '', $def = NULL, $callback = NULL, $id = NULL)
		{
			if ( $id == NULL ) {
				$id = 'list_' . substr(md5(rand()),0,5);
			}
			
			if ( $callback == NULL ) {
				$callback = 'function(){}';
			} else {
				$callback = 'function(){'.$callback.'}';
			}
			
			$html = <<< EOF
				<div class="list" id="$id">
					<div class="list_title">
						<div class="list_title_t">$title</div>
					</div>
					<div class="list_con"></div>
				</div>
				<script>ajax_loadPage('$def', $('#$id').find('.list_con'), '', $callback);</script>
EOF;
			return $html;
		}
		
		function list_table ($data = array())
		{
		
			global $USER;
		
			$title = $data['title'];
			$menu = $data['menu'];
			$def = $data['default'];
			$id = $data['id'];
			$dis = $data['display'];
			$callback = $data['callback'];
			
			if ( empty($id) ) {
				$id = 'list_' . substr(md5(rand()),0,5);
			}
			
			if ( empty($dis) ) {
				$dis = false;
			}
			
			if ( $dis == true ) {
				$display = "$('#$id').find('.list_title_m,.list_con').css('display', 'none');";
			}
			
			$i = 0;
			foreach ($menu as $key => $value) {
				
				if (empty($def)) {
				
					if ($i == 0) {
					
						$style = 'class="click"';
						$load = $i;
					
					} else {
					
						$style = '';
					
					}
				
				} else {
				
					if ($key == $def) {
				
						$style = 'class="click"';
				
						$load = $i;
				
					} else {
				
						$style = '';
				
					}
				
				}
				
				if (!empty($callback[$key])) {
					$callback_fn = 'function(){'.$callback[$key].'}';
				} else {
					$callback_fn = 'function(){}';
				}
								
				$queryString = preg_replace('/action\=.[^&]*&/i', '', $_SERVER['QUERY_STRING']);
				
				$queryString = preg_replace('/mod\=.[^&]*&/i', '', $queryString);
				
				$menu_list .= '<li '.$style.'><a href="javascript:;" key="' . $id . '" onclick="table_list_menu(\''.$value.'&'.$queryString.'&this='.$id.'\', $(\'#'.$id.'\').find(\'.list_con\'), this, '.$callback_fn.');">'.$key.'</a></li>';
				$i++;
			}
			
			$html = <<< EOF
				<div class="list" id="$id">
					<div class="list_title">
						<div class="list_title_t">$title</div>
						<div class="list_title_m">
							<ul>
								$menu_list
							</ul>
						</div>
					</div>
					<div class="list_con"></div>
				</div>
				<script>$('#$id').find('.list_title_m').find('a:eq($load)').click();$display</script>
EOF;
			return $html;
		}
		
		function table_list_oper ($href)
		{
			global $language;
			
			foreach ($href as $key => $v) {
				$icon .= '{'.$key.'}';
			}
			
			$id = Jpublic::get(this);
			$now_url = str_replace('mod=', '', Jpublic::all_get(array('rand','type')));
			$ref = $now_url;
			$div = '$(\'#' .$id. '\').find(\'.list_con\')';
			
			$delete_fn = 'layer.del(function () {table_delete(\''.$href['delete'].'\', \''.$ref.'\', '.$div.', this)});';

			$modify = '<a class="table_list_oper_m" title="'.$language['tag-title']['modify'].'" onclick="into(\''.$href['modify'].'\', \''.$id.'\')"></a>';
			$delete = '<a class="table_list_oper_d" title="'.$language['tag-title']['delete'].'" onclick="'.$delete_fn.'"></a>';
			
			$icon = str_replace('{modify}', $modify, $icon);
			$icon = str_replace('{delete}', $delete, $icon);
			$icon = str_replace('{DIY}', $href['DIY'], $icon);
			
			return '<div class="table_list_oper">'.$icon.'</div>';
			
		}
		
		function error ($title = '', $con = '')
		{
			$html = <<< _print
				<div class="list">
					<div class="list_title">
						<div class="list_title_t">$title</div>
					</div>
					<div class="list_con" style="text-align:center;height:40px;line-height:40px;">$con</div>
				</div>
_print;
			return $html;
		}
		
		function load_tip_html ($con = '', $href = '')
		{
			global $language;
			$location = $language['tip']['location'];
			$tip_time = $language['tip']['tip_time'];
			$html = <<< EOF
				<div class="list">
					<div class="list_title"></div>
					<div class="list_con" style="text-align:center;"><br/>$con<br/><br/>$tip_time<br/><br/><div class="tip_href"><a href="javascript:;" onclick="location.href='$href';">$location</a></div><br/></div>
				</div>
EOF;
			return $html;
		}
		
		function tip_html ($tip, $href = REFERER)
		{
			$href = urlencode($href);
			Jpublic::location(ACTION.'?action=public&mod=tip-html&href='.$href.'&tip='.$tip);
		}
		
		function tip ($data)
		{
			$type = $data['type'];
			$con = $data['title'];
			$href = $data['href'];
			$style = $data['style'];
			
			if (!empty($style)) {
				$style = 'style="'.$style.'"';
			}
			
			if ( $href == NULL ) {
				$toPage = '';
			} else {
				$toPage = 'onclick="toPage(\''.$href.'\');"';
			}
			
			switch ($type) {
				case 0:
					$html = '<div class="tip" '.$style.'><div class="tip_err"><div class="tip_err_tit"><a href="javascript:;" '.$toPage.'>'.$con.'</a></div><div class="tip_close"></div></div></div>';
				break;
				case 1:
					$html = '<div class="tip" '.$style.'><div class="tip_tip"><div class="tip_tip_tit"><a href="javascript:;" '.$toPage.'>'.$con.'</a></div><div class="tip_close"></div></div></div>';
				break;
				case 2:
					$html = '<div class="tip" '.$style.'><div class="tip_war"><div class="tip_war_tit"><a href="javascript:;" '.$toPage.'>'.$con.'</a></div><div class="tip_close"></div></div></div>';
				break;
				case 3:
					$html = '<div class="tip" '.$style.'><div class="tip_rig"><div class="tip_rig_tit"><a href="javascript:;" '.$toPage.'>'.$con.'</a></div><div class="tip_close"></div></div></div>';
				break;
			}
			
			return $html;
		}
		
		function checkBoxList($group, $name, $default) {
			
			$attrGroup = common::getAttr($group);
			
			$default = explode(',', $default);
			
			while ($dr = mysql_fetch_array($attrGroup)) {
			
				if (in_array($dr['attrValue'], $default)) {
				
					$checked = 'checked="checked"';
				
				}
			
				$html .= '<label><input type="checkbox" value="'.$dr['attrValue'].'" '.$checked.' name="'.$name.'[]" />'.$dr['attrHtml'].'</label>';
			
			}
			
			return $html;
			
		}
		
		function selectList($group, $name, $default) {
			
			$attrGroup = common::getAttr($group);
			
			while ($dr = mysql_fetch_array($attrGroup)) {
		
				if ($dr['attrValue'] == $default) {
				
					$selected = 'selected="selected"';
				
				}
			
				$html .= '<option '.$selected.' value="'.$dr['attrValue'].'">'.$dr['attrHtml'].'</option>';
			
			}
			
			return '<select name="'.$name.'">' . $html . '</select>';
			
		}
		
		function paging ($sql, $total, $page, $menu = '{list}{top}{search}', $top = array('list' => array(10,20,30,40,50), 'default' => 20), $show = 4)
		{
			global $language;
			global $PAGING;
			
			$top_def = Jpublic::get('top');
			$search = rawurlencode(Jpublic::get('search', 0, 2));
			empty($top_def) && $top_def = $top['default'];
			$id = Jpublic::get(this);
			$div = '$(\'#' .$id. '\').find(\'.list_con\')';
			
			$now_url = str_replace('mod=', '', Jpublic::all_get(array('page','rand','this','type','top','search')));
			$href = $now_url . '&page={page}&search='.$search.'&top='.$top_def.'&this=' . $id;
			
			$paging_array = array(
				'total' => $total, //总条数
				'top' => $top_def, //每页显示条数
				'show' => $show, //分页导航长度
				'home_page' => "<li class=\"paging_n\"><a href=\"javascript:;\" onclick=\"ajax_loadPage('$href', $div)\">".$language[paging][homepage]."</a></li></li>",
				'list_page' => "<li class=\"paging_n\"><a href=\"javascript:;\" onclick=\"ajax_loadPage('$href', $div)\">{page}</a></li>",
				'list_page_now' => "<li class=\"paging_s\">{page}</li>",
				'up_page' => "<li class=\"paging_n\"><a href=\"javascript:;\" onclick=\"ajax_loadPage('$href', $div)\">".$language[paging][up_page]."</a></li>",
				'down_page' => "<li class=\"paging_n\"><a href=\"javascript:;\" onclick=\"ajax_loadPage('$href', $div)\">".$language[paging][down_page]."</a></li>",
				'last_page' => "<li class=\"paging_n\"><a href=\"javascript:;\" onclick=\"ajax_loadPage('$href', $div)\">".$language[paging][last_page]."</a></li>",
				'style' => '{home_page}{up_page}{paging_list}{last_page}{down_page}',
				'page' => $page, //当前页
				'sql' => $sql,
			);
			$paging = $PAGING -> init($paging_array);
			
			foreach ($top['list'] as $k) {
				$selected = '';
				$k == $top_def && $selected = 'selected="selected"';
				$select .= '<option value="'.$k.'" '.$selected.'>'.$k.'条/页</option>';
			}
			
			$select = "<div class=\"top_list\"><select onchange=\"ajax_loadPage('$href&top='+this.value, $div);\">".$select."</select></div>";
			
			if ($search != '') {
				$search_bg = 'style="background-image:none;"';
				$button_dp = '';
			} else {
				$search_bg = '';
				$button_dp = 'style="display:none;"';
			}
			
			$search = urldecode($search);
			
			$search = "<div class=\"search\" $search_bg ><form onsubmit=\"ajax_loadPage('$href&search='+encodeURI($(this).parent().find('input').val()), $div);return false;\"><input onclick=\"this.select()\" onkeyup=\"paging_search(this, 'url(style/images/bg-search-font.gif)');\" class=\"text\" type=\"text\" maxlength=\"10\" value=\"$search\" /><button class=\"submit\" type=\"submit\" $button_dp >".$language['public']['search']."</button></form></div>";
			
			$menu = str_replace('{top}', $select, $menu);
			$menu = str_replace('{search}', $search, $menu);
			$menu = str_replace('{list}', "<ul>$paging[paging]</ul>", $menu);
			
			return array('sql' => $paging['sql'], 'list' => "<div class=\"paging\">$menu</div>");
		}
		
	}
?>